﻿Imports System.Management
Public Class GetShares
    Public Shared Function GetShares(ByVal strcomputer As String) As List(Of String)
        Dim wmiScope As ManagementScope
        Dim wmiConnectionOptions As New ConnectionOptions
        Dim query As ManagementObjectSearcher
        Dim queryCollection As ManagementObjectCollection
        Dim oq As ObjectQuery
        Dim shares As New List(Of String)
        ' ReSharper disable once RedundantAssignment
        With wmiConnectionOptions
            .Impersonation = ImpersonationLevel.Impersonate
            .Authentication = AuthenticationLevel.Packet
        End With
        Try
            wmiScope = New ManagementScope("\\" & strcomputer & "\root\cimv2", wmiConnectionOptions)
            oq = New ObjectQuery("select * from Win32_Share")
            query = New ManagementObjectSearcher(wmiScope, oq)
            queryCollection = query.Get()
            For Each oReturn As ManagementObject In queryCollection
                shares.Add(oReturn("name").ToString())
            Next
        Catch
            'Return "Machine is inaccessable"
        End Try
        Return shares
    End Function
End Class
